package algorithm.leetcode.I101to200;

public class Q125 {
    public boolean isPalindrome(String s) {
        if (s.length() == 0) return true;
        int left = 0, right = s.length()-1;
        while (left < right) {
            if (!isCharacterOrNumber(s.charAt(left))) left++;
            else if (!isCharacterOrNumber(s.charAt(right))) right--;
            else {
                // 这里注意不能用 Math.abs(s.charAt(left) - s.charAt(right)) == 32,会有数字的ascii码+32=字母的情况
                if (Character.toLowerCase(s.charAt(left)) == Character.toLowerCase(s.charAt(right))) {
                    left++;
                    right--;
                } else return false;
            }
        }
        return true;
    }

    private static boolean isCharacterOrNumber(char c) {
        boolean isCapital = c >= 65 && c <= 90;
        boolean isLowercase = c >= 97 && c <= 122;
        boolean isNumber = c >= 48 && c <= 57;
        return isCapital ||  isLowercase || isNumber;
    }

    public static void main(String[] args) {
        Q125 q125 = new Q125();
        System.out.println(q125.isPalindrome("0P"));
    }
}
